package com.lottery.im.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lottery.im.R;
import com.lottery.im.api.Api;
import com.lottery.im.api.result.entity.SingleRedPacketsReceived;
import com.lottery.im.util.transformations.CropCircleTransformation;
import java.text.DecimalFormat;

/**
 * 单个红包已被领取 对话框
 */

public class RedPackets1DialogBuilder extends Dialog {

  private static Context tmpContext;
  private static RedPackets1DialogBuilder instance;

  private View mDialogView;
  private RelativeLayout mLayoutAll;
  private ImageView mIvPortrait;
  private TextView mTvFromName;
  private TextView mTvTip;
  private TextView mTvMoney;
  private LinearLayout mLayoutMoney;
  private View.OnClickListener mOnDetailClick;

  public RedPackets1DialogBuilder(Context context) {
    super(context);
    init(context);
  }

  public RedPackets1DialogBuilder(Context context, int theme) {
    super(context, theme);
    init(context);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WindowManager.LayoutParams params = getWindow().getAttributes();
    //params.height = ViewGroup.LayoutParams.MATCH_PARENT;
    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
    getWindow().setAttributes(params);
    setCancelable(true);
    setCanceledOnTouchOutside(true);
  }

  public static RedPackets1DialogBuilder getInstance(Context context) {
    if (instance == null || !tmpContext.equals(context)) {
      synchronized (RedPackets1DialogBuilder.class) {
        if (instance == null || !tmpContext.equals(context)) {
          instance = new RedPackets1DialogBuilder(context, R.style.dialog_untran);
        }
      }
    }
    tmpContext = context;
    return instance;
  }

  private void init(Context context) {

    mDialogView = View.inflate(context, R.layout.dialog_redpackets1, null);
    mLayoutAll = mDialogView.findViewById(R.id.layout_all);

    setContentView(mDialogView);

    //this.setOnShowListener(new OnShowListener() {
    //  @Override
    //  public void onShow(DialogInterface dialogInterface) {
    //    BaseEffects animator = new ZoomEffects();
    //    animator.start(mLayoutAll);
    //  }
    //});

    mLayoutAll.setOnClickListener(view -> dismiss());

    mDialogView.findViewById(R.id.layout_dialog).setOnClickListener(view -> {
    });

    mIvPortrait = mDialogView.findViewById(R.id.iv_portrait);
    mTvFromName = mDialogView.findViewById(R.id.tv_from_name);
    mTvTip = mDialogView.findViewById(R.id.tv_tip);
    mTvMoney = mDialogView.findViewById(R.id.tv_money);
    mLayoutMoney = mDialogView.findViewById(R.id.layout_money);
    mDialogView.findViewById(R.id.tv_detail).setOnClickListener(v -> {
      if (null != mOnDetailClick) mOnDetailClick.onClick(v);
      dismiss();
    });
  }

  public RedPackets1DialogBuilder setContent(SingleRedPacketsReceived received,
      View.OnClickListener onClickListener) {
    Glide.with(tmpContext)
        .load(Api.BASEADDRESS + received.getUserImg())
        .apply(new RequestOptions()
            .bitmapTransform(new CropCircleTransformation())
            .placeholder(R.drawable.icon_conversation_de)
            .error(R.drawable.icon_conversation_de))
        .into(mIvPortrait);
    mTvFromName.setText("来自" + received.getUserName() + "的红包");
    this.mOnDetailClick = onClickListener;
    return this;
  }

  public RedPackets1DialogBuilder setTip(String tip) {
    mTvTip.setText(tip);
    return this;
  }

  public RedPackets1DialogBuilder setSucContent(SingleRedPacketsReceived received,
      View.OnClickListener onClickListener) {
    mTvTip.setVisibility(View.GONE);
    mLayoutMoney.setVisibility(View.VISIBLE);
    DecimalFormat df = new DecimalFormat("#0.00");
    mTvMoney.setText(df.format(received.getMoney()));
    Glide.with(tmpContext)
        .load(Api.BASEADDRESS + received.getUserImg())
        .apply(new RequestOptions()
            .bitmapTransform(new CropCircleTransformation())
            .placeholder(R.drawable.icon_conversation_de)
            .error(R.drawable.icon_conversation_de))
        .into(mIvPortrait);
    mTvFromName.setText("来自" + received.getUserName() + "的红包");
    this.mOnDetailClick = onClickListener;
    return this;
  }

  @Override public void dismiss() {
    super.dismiss();
    instance = null;
    tmpContext = null;
  }

  @Override
  public void show() {
    super.show();
  }
}
